var lastId;

function getMessages() {
    performQuery("messages", "GET", function (data, textStatus, jqXHR) {
        if (data.status == 'ERROR') {
            alert('Error: ' + data.description);
        } else {
            lastId = data.messages[data.messages.length - 1].id;
            show(data.messages);
            messages = data.messages;
        }
    });
}

function getLast() {
    performQuery("messages/older/" + lastId, "GET", function (data, textStatus, jqXHR) {
            if (data.messages.length == 0) {
                return;
            }
            if (data.status == 'ERROR') {
                alert('Error: ' + data.description);
            } else {
                lastId = data.messages[data.messages.length - 1].id;
                add(data.messages);
            }
        }
    );
}

function addMessage(message, callback) {
    timer.pause();
    var text = { "text":message };
    var json = JSON.stringify(text);
    performQuery("messages", "POST", function (data) {
        if (data.status == 'ERROR') {
            //alert('Error: ' + data.description);
        }
        timer.resume();
        callback();
    }, json);

}
function getMessageText() {
    var text = '';
    var quote = $('#quote');
    if (quote.css('display') != 'none') {
        text += "[quote author='" +
            quote.find('.quoteauthor').html() + "' date='" + quote.find('.quotedate').html() +
            "']" + replaceSmiles(quote.find('.quotetext').html()) +
            "[/quote]";
    }

    text += $('#text').val();
    return text;
}
function add(messages) {
    var messageContainer = $('#messages');
    for (message in messages) {
        var text = messages[message].text;

        for (smile in smiles) {
            text = text.replaceAll(smile, "<img class='smile' title='"+smile+"' src='/public/img/smile/" + smiles[smile] + "'>")
        }
        var quoteRegexp = /^\[quote author='([a-zA-ZА-Яа-я.0-9 _-]+)' date='([a-zA-ZА-Яа-я:.0-9 _-]+)'\]((.|\n)+)\[\/quote\](.*)/;
        var quote = "";
        if (text.match(quoteRegexp)) {
            quote = text.replace(quoteRegexp, "<li class='quote'>" +
                "<div class='quotetext'>$3</div>" +
                "<div class='quoteauthor'>$1</div>" +
                "<div class='quotedate'>$2</div>" +
                "</li>");
        }
        text = text.replace(quoteRegexp, "$5");
        var user = messages[message].user;
        var elements = '<ul class="message"><li class="messageuser user">' + user + '</li>' +
            '<li class="messagedate">' + messages[message].date +
            '</li>' + quote +
            '<li class="messagetext';
        if (text.indexOf(login + ', ') >= 0) {
            elements += ' messageforyou';
        }

        elements += '">' +
            text + '</li></ul>';
        messageContainer.append(elements);
    }
    updateBindings();
    $(window).scrollTo('100%', 1);
}